package com.ruikong.dazhi.controller.basicinfo;


import com.github.pagehelper.PageInfo;
import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.basicinfo.Product;
import com.ruikong.dazhi.model.input.ProductPageInput;
import com.ruikong.dazhi.model.output.PersonnelOutput;
import com.ruikong.dazhi.service.basicinfo.IProductService;
import com.ruikong.dazhi.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.yaml.snakeyaml.events.Event;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.bind.ValidationEvent;

@RestController
@RequestMapping("/data")
@EnableAutoConfiguration
@Api(tags = "产品管理")
public class ProductController {


    @Resource
    private IProductService iProductService;


    private final  static Logger logger= LoggerFactory.getLogger(ProductController.class);



    @PostMapping("/getProductPageInfo")
    @ApiOperation("产品信息-分页")
    public PageInfo getProductPageInfo(@RequestBody  ProductPageInput input){
        PageInfo pageInfo=null;
        try {
             pageInfo=this.iProductService.productPageInfo(input);
        }catch (Exception e){
            e.printStackTrace();
          logger.error("getProductPageInfo{}",e.getMessage());
        }
        return pageInfo;
    }


    @PostMapping("/addProduct")
    @ApiOperation("新增产品")
    public BaseResult addProduct(@RequestBody Product product){
        BaseResult baseResult=new BaseResult();
        try {
            if (this.iProductService.isExist(product)){
                baseResult.setCode(ResultApi.errorCode);
                baseResult.setMsg("该产品编号已经存在!");
                return baseResult;
            }
            if(this.iProductService.insert(product)>0){
                baseResult.setCode(ResultApi.successCode);
                baseResult.setMsg(ResultApi.addSuccessMsg);
            }
            else {
                baseResult.setCode(ResultApi.errorCode);
                baseResult.setMsg(ResultApi.addFailMsg);
            }
        }catch (Exception e){
            baseResult.setCode(ResultApi.errorCode);
            baseResult.setMsg(ResultApi.addFailMsg);
            logger.error("addProduct{}",e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }


    @PostMapping("/updateProduct")
    @ApiOperation("编辑产品")
    public BaseResult updateProduct(@RequestBody Product product){
        BaseResult baseResult=new BaseResult();
        try {

            if (this.iProductService.isExist(product)){
                baseResult.setCode(ResultApi.errorCode);
                baseResult.setMsg("该产品编号已经存在!");
                return baseResult;
            }
            if (this.iProductService.update(product)>0){
                baseResult.setCode(ResultApi.successCode);
            }
            else {
                baseResult.setCode(ResultApi.errorCode);
            }
            baseResult.setMsg(ResultApi.updateSuccessMsg);
        }catch (Exception e){
            baseResult.setCode(ResultApi.errorCode);
            baseResult.setMsg(ResultApi.updateSuccessMsg);
            logger.error("updateProduct{}",e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }


    @PostMapping("/deleteProduct")
    @ApiOperation("删除产品")
    public BaseResult deleteProduct(@RequestBody Product product){
        BaseResult baseResult=new BaseResult();
        try {
            if (this.iProductService.deleteById(product.getId())){
                baseResult.setCode(ResultApi.successCode);
            }
            else {
                baseResult.setCode(ResultApi.errorCode);
            }
            baseResult.setMsg(ResultApi.deleteSuccessMsg);
        }catch (Exception e){
            baseResult.setCode(ResultApi.errorCode);
            baseResult.setMsg(ResultApi.deleteSuccessMsg);
            logger.error("deleteProduct{}",e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }



    @PostMapping("/getProductById")
    @ApiOperation("根据id查询单个产品")
    public BaseResult getProductById(@RequestBody Product product){
        BaseResult baseResult=new BaseResult();
        try {
            product=this.iProductService.queryById(product.getId());
            baseResult.setCode(ResultApi.successCode);
            baseResult.setData(product);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("getProductById{}",e.getMessage());
            baseResult.setCode(ResultApi.errorCode);
            baseResult.setMsg(ResultApi.operateFailMsg);
        }
        return baseResult;
    }




    @GetMapping("/downProductExcelTemplate")
    @ApiOperation("产品导入模版下载")
    public void downProductExcelTemplate(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileName") String fileName) {
        String[] columNames = {"产品编号", "产品名称", "产品批次","简称", "性状", "制造日期"};
        ExcelUtil.download(response, request, fileName, columNames);
    }

 
    @PostMapping("/productImport")
    @ApiOperation("产品信息导入")
    public BaseResult productImport(@RequestParam("file") MultipartFile multipartFile) {
        BaseResult baseResult = new BaseResult();
        try {
            baseResult = this.iProductService.productImport(multipartFile);
        } catch (Exception e) {
            logger.error("productImport", e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }



}
